package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateDeviceWhiteListDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateDeviceWhiteListSaveDTO;
import com.itlong.cloud.POJO.PO.DeviceWhiteListInfoPO;
import com.itlong.cloud.POJO.VO.operate.DeviceWhiteListDetailVO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceWhiteListVO;
import com.itlong.cloud.POJO.VO.operate.OperateMacInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.OperateDeviceActivateStatusEnum;
import com.itlong.cloud.operate.dao.IOperateDeviceWhiteListDao;
import com.itlong.cloud.operate.service.IOperateDeviceWhiteListService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <desc>
 *      运营-设备白名单管理服务接口实现类
 * </desc>
 *
 * @createDate2019/12/11
 **/
@Service
public class OperateDeviceWhiteListServiceImpl implements IOperateDeviceWhiteListService {

    @Autowired
    private IOperateDeviceWhiteListDao iOperateDeviceWhiteListDao;

    /**
     * <desc>
     *      分页查询设备白名单信息
     * </desc>
     *
     * @param operateDeviceWhiteListDTO 查询参数DTO
     * @return  设备白名单分页数据
     * @Author Zhu.yj
     * @createDate  2019/12/11
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateDeviceWhiteListVO> getWhiteListByPage(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        if (StringUtils.isBlank(operateDeviceWhiteListDTO.getOrderColumn())){
            operateDeviceWhiteListDTO.setOrderColumn("createTime");
            operateDeviceWhiteListDTO.setOrderRule("DESC");
        }
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeviceWhiteListDTO);
        Page<OperateDeviceWhiteListVO> page = new Page<>();
        page.getPage().setPageSize(operateDeviceWhiteListDTO.getPageSize());
        page.getPage().setCurrentPage(operateDeviceWhiteListDTO.getCurrentPage());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        page.setRows(iOperateDeviceWhiteListDao.getWhiteListByPage(params),iOperateDeviceWhiteListDao.getWhiteListPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      保存设备白名单信息
     * </desc>
     *
     * @param operateDeviceWhiteListDTO 参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2019/12/12
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer save(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        Date nowTime = new Date();
        DeviceWhiteListInfoPO deviceWhiteListInfoPO = new DeviceWhiteListInfoPO();
        BeanUtils.copyProperties(operateDeviceWhiteListDTO, deviceWhiteListInfoPO);
        deviceWhiteListInfoPO.setWhiteListId(LogicIdUtil.bussinessId());
        deviceWhiteListInfoPO.setMacAddress(operateDeviceWhiteListDTO.getMacAddress());
        deviceWhiteListInfoPO.setRemark(operateDeviceWhiteListDTO.getRemark());
        deviceWhiteListInfoPO.setCreateTime(nowTime);
        deviceWhiteListInfoPO.setUpdateTime(nowTime);
        deviceWhiteListInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
        deviceWhiteListInfoPO.setCreateUserId(operateDeviceWhiteListDTO.getLoginUserId());
        deviceWhiteListInfoPO.setOperateUserId(operateDeviceWhiteListDTO.getLoginUserId());
        return iOperateDeviceWhiteListDao.save(SqlUtil.durableData(deviceWhiteListInfoPO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      禁用或删除设备白名单
     * </desc>
     *
     * @param operateDeviceWhiteListDTO 参数DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2019/12/12
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer forbidden(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        Integer activateStatus = iOperateDeviceWhiteListDao.getActivateStatus(operateDeviceWhiteListDTO.getWhiteListId());
        //删除操作时，已激活(activateStatus = 1)的返回错误码-1
        if (activateStatus != null && activateStatus.equals(OperateDeviceActivateStatusEnum.ACTIVATE.getType())
                && operateDeviceWhiteListDTO.getOperType().equals(DataStatusEnum.DELETE.getType())){
            return -1;
        }
        Date nowTime = new Date();
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeviceWhiteListDTO);
        params.put("status",operateDeviceWhiteListDTO.getOperType());
        params.put("updateTime",nowTime);
        params.put("operateUserId",operateDeviceWhiteListDTO.getLoginUserId());
        return iOperateDeviceWhiteListDao.update(params);
    }

    /**
     * <desc>
     *      获取单个设备白名单信息
     * </desc>
     *
     * @param mac mac地址
     * @return
     * @Author Zhu.yj
     * @createDate  2019/12/16
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateDeviceWhiteListVO getWhiteListByMac(String mac) throws Exception{
        return iOperateDeviceWhiteListDao.getWhiteListByMac(mac);
    }

    /**
     * <desc>
     *      获取设备白名单信息状态
     * </desc>
     *
     * @param deviceUnique 设备编号
     * @return
     * @Author Zhu.yj
     * @createDate  2019/12/23
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public Integer getWhiteListStatus(String deviceUnique) throws Exception{
        Integer whiteListStatus =  iOperateDeviceWhiteListDao.getWhiteListStatusByDeviceUnique(deviceUnique);
        //规避空指针异常
        if (whiteListStatus == null){
            whiteListStatus = 0;
        }
        return whiteListStatus;
    }

    /**
     * <desc>
     *      获取设备白名单详情
     * </desc>
     *
     * @param whiteListId 白名单id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/8/11
     */
    @Override
    public DeviceWhiteListDetailVO getDetail(String whiteListId) throws Exception {
        return iOperateDeviceWhiteListDao.getDetail(whiteListId);
    }

    /**
     * <desc>
     *      获取所有mac地址
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2020/8/11
     */
    @Override
    public List<OperateMacInfoVO> getMacList() throws Exception {
        return iOperateDeviceWhiteListDao.getMacList();
    }

    /**
     * <desc>
     *      批量导入设备白名单列表
     * </desc>
     *
     * @param whiteListSaveDTOList 设备白名单列表
     * @return
     * @Author Zhu.yj
     * @createDate  2020/8/11
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer importDeviceWhiteList(List<OperateDeviceWhiteListSaveDTO> whiteListSaveDTOList) throws Exception{
        Date nowTime = new Date();
        List<DeviceWhiteListInfoPO> savePOList = new ArrayList<>();
        for (OperateDeviceWhiteListSaveDTO operateDeviceWhiteListSaveDTO : whiteListSaveDTOList){
            DeviceWhiteListInfoPO deviceWhiteListInfoPO = new DeviceWhiteListInfoPO();
            deviceWhiteListInfoPO.setWhiteListId(LogicIdUtil.bussinessId());
            deviceWhiteListInfoPO.setDeviceType(operateDeviceWhiteListSaveDTO.getDeviceType());
            deviceWhiteListInfoPO.setMacAddress(operateDeviceWhiteListSaveDTO.getMacAddress());
            deviceWhiteListInfoPO.setRemark(operateDeviceWhiteListSaveDTO.getRemark());
            deviceWhiteListInfoPO.setCreateUserId(operateDeviceWhiteListSaveDTO.getLoginUserId());
            deviceWhiteListInfoPO.setOperateUserId(operateDeviceWhiteListSaveDTO.getLoginUserId());
            deviceWhiteListInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
            deviceWhiteListInfoPO.setCreateTime(nowTime);
            deviceWhiteListInfoPO.setUpdateTime(nowTime);
            savePOList.add(deviceWhiteListInfoPO);
        }
        return iOperateDeviceWhiteListDao.batchSaveDeviceWhiteList(savePOList);
    }

    /**
     * <desc>
     * 根据上传的mac地址和授权文件id更新白名单中的设备激活状态
     * </desc>
     *
     * @param operateDeviceWhiteListDTO
     * @Author chengshican
     * @Date 2020-08-16 10:14
     * @Param operateDeviceWhiteListDTO 参数
     * @Return
     * @Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateStatusByAuthIdAndMAC(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeviceWhiteListDTO);
        params.put("activateStatus",1);
        params.put("operateUserId","deviceUpdate");
        params.put("activateTime", df.parse(operateDeviceWhiteListDTO.getActivateTime()));
        return iOperateDeviceWhiteListDao.updateStatusByAuthIdAndMAC(params);
    }

    /**
     * <desc>
     * 根据上传的mac地址更新白名单中的设备异常标记和是否转换为设备白名单
     * </desc>
     *
     * @param operateDeviceWhiteListDTO
     * @Author chengshican
     * @Date 2020-09-02 14:53
     * @Param operateDeviceWhiteListDTO 参数
     * @Return
     * @Exception
     */
    @Override
    public int updateFlagByMAC(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateDeviceWhiteListDTO);
        return iOperateDeviceWhiteListDao.updateStatusByAuthIdAndMAC(params);
    }

    /**
     * <desc>
     *      添加备注
     * </desc>
     *
     * @param operateDeviceWhiteListDTO 设备白名单DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/9/2
     */
    @Override
    public Integer update(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        Map<String, Object> params = new HashMap<>();
        DeviceWhiteListInfoPO deviceWhiteListInfoPO = new DeviceWhiteListInfoPO();
        Date nowTime = new Date();
        BeanUtils.copyProperties(operateDeviceWhiteListDTO, deviceWhiteListInfoPO);
        deviceWhiteListInfoPO.setUpdateTime(nowTime);
        deviceWhiteListInfoPO.setOperateUserId(operateDeviceWhiteListDTO.getLoginUserId());
        return iOperateDeviceWhiteListDao.updateChangeFlag(SqlUtil.durableData(deviceWhiteListInfoPO, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      转换为白名单
     * </desc>
     *
     * @param operateDeviceWhiteListDTO 设备白名单DTO
     * @return 受影响行数
     * @Author Zhu.yj
     * @createDate  2020/9/2
     */
    @Override
    public Integer convertToWhiteList(OperateDeviceWhiteListDTO operateDeviceWhiteListDTO) throws Exception {
        Map<String, Object> params = new HashMap<>();
        DeviceWhiteListInfoPO deviceWhiteListInfoPO = new DeviceWhiteListInfoPO();
        Date nowTime = new Date();
        BeanUtils.copyProperties(operateDeviceWhiteListDTO, deviceWhiteListInfoPO);
        deviceWhiteListInfoPO.setUpdateTime(nowTime);
        deviceWhiteListInfoPO.setOperateUserId(operateDeviceWhiteListDTO.getLoginUserId());
        deviceWhiteListInfoPO.setChangeFlag(DataStatusEnum.NORMAL.getType());
        return iOperateDeviceWhiteListDao.updateChangeFlag(SqlUtil.durableData(deviceWhiteListInfoPO, PlatformConstants.TABLE_UPDATE));
    }
}
